package com.weetop.cfw.home_page.fragment

import com.blankj.utilcode.util.ActivityUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.AudioSearchResultPresenterImp
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.base.view.AudioSearchResultView
import com.weetop.cfw.bean.AudioVideoBrowsingRecordBean
import com.weetop.cfw.bean.TestBean
import com.weetop.cfw.constants.Constants
import com.weetop.cfw.event.VideoAndAudioSearchResultEvent
import com.weetop.cfw.home_page.activity.CloudCurriculumAndAudioSearchResultActivity
import com.weetop.cfw.mine.activity.AudioDetailActivity
import com.weetop.cfw.mine.adapter.MyCourseListAdapter
import com.weetop.cfw.utils.LogInUtils
import kotlinx.android.synthetic.main.fragment_cloud_curriculum_and_audio_search_result.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 管理音频搜索结果界面
 */
class AudioSearchResultFragment: CommonBaseFragment(),AudioSearchResultView,OnRefreshLoadMoreListener {

    private lateinit var myCourseListAdapter:MyCourseListAdapter

    private lateinit var audioSearchResultPresenterImp: AudioSearchResultPresenterImp

    private var currentPageNumber = 1//当前的数据页数
    private var totalCount = 0//数据的总数量
    private var classid = 0//筛选分类的ID，0表示无限

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否加载更多

    private val audioVideoDataList = ArrayList<AudioVideoBrowsingRecordBean.DataBean>()

    override fun getLayoutId() = R.layout.fragment_cloud_curriculum_and_audio_search_result

    override fun initView() {
        audioSearchResultPresenterImp = AudioSearchResultPresenterImp()
        audioSearchResultPresenterImp.attachView(this)

        smartRefreshLayout.setOnRefreshLoadMoreListener(this)
    }

    override fun initData() {
        classid = Constants.audioClassId

        myCourseListAdapter = MyCourseListAdapter(R.layout.layout_item_audio_frequency,audioVideoDataList)
        myCourseListAdapter.emptyView = emptyView
        myCourseListAdapter.setOnItemClickListener { _, _, position ->
            if(LogInUtils.userIsLogIn()){
                AudioDetailActivity.startAudioDetailActivity(context!!,audioVideoDataList[position].infoid)
            }
        }
        cloudCurriculumAndAudioSearchResultRV.adapter = myCourseListAdapter
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onReceiveVideoAndAudioSearchResultEvent(videoAndAudioSearchResultEvent: VideoAndAudioSearchResultEvent){
        if(videoAndAudioSearchResultEvent.selectPosition == 1){
            smartRefreshLayout.autoRefresh()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        audioSearchResultPresenterImp.detachView()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(totalCount <= audioVideoDataList.size){
            showNativeShortToast("暂无更多数据")
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadMore = true
            audioSearchResultPresenterImp.getAudioSearchResultDataList(this,classid,(activity!! as CloudCurriculumAndAudioSearchResultActivity).keyword,currentPageNumber + 1)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        audioSearchResultPresenterImp.getAudioSearchResultDataList(this,classid,(activity!! as CloudCurriculumAndAudioSearchResultActivity).keyword,1)
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        if(isFirstLoadData){
            audioSearchResultPresenterImp.getAudioSearchResultDataList(this,classid,(activity!! as CloudCurriculumAndAudioSearchResultActivity).keyword,currentPageNumber)
        }
    }

    /**
     * 管理音频数据列表获取成功
     */
    override fun audioFrequencyListDataGetSuccess(audioVideoBrowsingRecordBean: AudioVideoBrowsingRecordBean?) {
        if(audioVideoBrowsingRecordBean != null){
            totalCount = audioVideoBrowsingRecordBean.records

            isFirstLoadData = false
            if(isRefresh){
                audioVideoDataList.clear()
                audioVideoDataList.addAll(audioVideoBrowsingRecordBean.data)
                myCourseListAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishRefresh(true)
                if(audioVideoBrowsingRecordBean.data.size > 0){
                    currentPageNumber = 1//将当前数据页数重置为1
                }
                isRefresh = false
                return
            }
            if(isLoadMore){
                audioVideoDataList.addAll(audioVideoBrowsingRecordBean.data)
                myCourseListAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishLoadMore(false)
                if(audioVideoBrowsingRecordBean.data.size > 0){
                    currentPageNumber += 1//将当前数据页数增加1
                }
                isLoadMore = false
                return
            }
            audioVideoDataList.addAll(audioVideoBrowsingRecordBean.data)
            myCourseListAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 管理音频数据列表获取失败
     */
    override fun audioFrequencyListDataGetFailed() {
        smartRefreshLayout.finishLoadMore(false)
        smartRefreshLayout.finishRefresh(false)
    }
}